package com.songlin.houxin.admin.service.impl;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlin.houxin.admin.api.dto.EnterprisesAddDTO;
import com.songlin.houxin.admin.api.dto.EnterprisesDeleteDTO;
import com.songlin.houxin.admin.api.dto.EnterprisesListDTO;
import com.songlin.houxin.admin.api.dto.EnterprisesUpdateDTO;
import com.songlin.houxin.admin.api.entity.Enterprises;
import com.songlin.houxin.admin.api.vo.BasePageVO;
import com.songlin.houxin.admin.api.vo.EnterprisesVO;
import com.songlin.houxin.admin.mapper.EnterprisesMapper;
import com.songlin.houxin.admin.service.EnterprisesService;
import com.songlin.houxin.common.core.exception.BusinessAssert;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author jinYuLinLang
 * @version 1.0.0
 * @ClassName EnterprisesServiceImpl.java
 * @Description <p></p>
 * @createTime 2022-03-06 11:49:00
 */
@Service
public class EnterprisesServiceImpl extends ServiceImpl<EnterprisesMapper, Enterprises> implements EnterprisesService {


    /**
     * @param dto 入参信息
     * @return
     */
    @Override
    public BasePageVO<EnterprisesVO> queryEnterprisesList(EnterprisesListDTO dto) {
        LambdaQueryWrapper<Enterprises> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(dto.getAbbreviation())) {
            wrapper.like(Enterprises::getAbbreviation, dto.getAbbreviation());
        }
        if (StrUtil.isNotBlank(dto.getAddress())) {
            wrapper.like(Enterprises::getAddress, dto.getAddress());
        }
        if (StrUtil.isNotBlank(dto.getMobile())) {
            wrapper.eq(Enterprises::getMobile, dto.getMobile());
        }
        if (StrUtil.isNotBlank(dto.getEnterprisesName())) {
            wrapper.like(Enterprises::getEnterprisesName, dto.getEnterprisesName());
        }
        Page<Enterprises> page = this.page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        return BasePageVO.instance(JSONUtil.toList(JSONUtil.toJsonStr(page.getRecords()), EnterprisesVO.class), page.getTotal());
    }

    /**
     * @param dto 入参信息
     */
    @Override
    public void deleteBatchEnterprises(EnterprisesDeleteDTO dto) {
        this.removeBatchByIds(dto.getEnterprisesIds());
    }

    /**
     * @param dto 入参信息
     */
    @Override
    public void updateEnterprises(EnterprisesUpdateDTO dto) {
        Enterprises enterprises = this.getById(dto.getId());
        BusinessAssert.notNull(enterprises, "企业信息不存在或者主键值出错");
        BeanUtils.copyProperties(dto, enterprises);
        this.updateById(enterprises);
    }


    /**
     * @param dto 入参信息
     */
    @Override
    public void addEnterprises(EnterprisesAddDTO dto) {
        Enterprises enterprises = new Enterprises();
        BeanUtils.copyProperties(dto, enterprises);
        this.save(enterprises);
    }
}
